package utils

import (
	"fmt"
	"github.com/go-ini/ini"
)

// 全局变量
var (
	AppMode  string
	HttpPost string
	JwtKey   string

	DbType     string
	DbUser     string
	DbHost     string
	DbPort     string
	DbPassWord string
	DbName     string

	AccessKey   string
	SecretKey   string
	Bucket      string
	QiniuServer string
)

// init方法在程序启动时会自动执行
func init() {
	file, error := ini.Load("config/config.ini")
	if error != nil {
		fmt.Println("配置文件读取错误，请检查文件路径：", error)
	}
	LoadServer(file)
	LoadData(file)
	LoadStorage(file)
}

func LoadServer(file *ini.File) {
	// MustString表示默认值，当拿不到数据时使用默认值
	AppMode = file.Section("server").Key("AppMode").MustString("debug")
	HttpPost = file.Section("server").Key("HttpPost").MustString("8000")
	JwtKey = file.Section("server").Key("JwtKey").MustString("1wjihi2i2nbsi2")
}

func LoadData(file *ini.File) {
	DbType = file.Section("database").Key("DbType").MustString("mysql")
	DbUser = file.Section("database").Key("DbUser").MustString("root")
	DbHost = file.Section("database").Key("DbHost").MustString("139.224.247.174")
	DbPort = file.Section("database").Key("DbPort").MustString("3306")
	DbPassWord = file.Section("database").Key("DbPassWord").MustString("123456")
	DbName = file.Section("database").Key("DbName").MustString("blog")
}

func LoadStorage(file *ini.File) {
	AccessKey = file.Section("storage").Key("AccessKey").String()
	SecretKey = file.Section("storage").Key("SecretKey").String()
	Bucket = file.Section("storage").Key("Bucket").String()
	QiniuServer = file.Section("storage").Key("QiniuServer").String()
}
